package com.xoomworks.rsfr.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

import com.xoomworks.rsfr.core.service.ResourceService;
import com.xoomworks.rsfr.model.Resource;

/**
 * Resource Service Implementation.
 * @author marius.boeru
 */
@Service
public class ResourceServiceImpl extends BaseServiceImpl<Resource> implements ResourceService {

    /**
     * Constructor.
     */
    public ResourceServiceImpl() {
        setClazz(Resource.class);
        setEntityUri("Resources");
        setClazzArray(Resource[].class);
    }

    @Override
    public List<Resource> getByUserId() {
        HttpHeaders headers = new HttpHeaders();
        AuthenticationServiceImpl.setAuthenticationHeader(headers);
        HttpEntity<Resource> requestEntity = new HttpEntity<Resource>(null, headers);

        List<Resource> resourceList = new ArrayList<Resource>();
        Resource[] resources = null;
        try {
            ResponseEntity<Resource[]> returnedEntity = restTemplate.exchange(BASE_URL + "/"
                    + entityUri + "/Get/MyResources", HttpMethod.GET, requestEntity,
                    Resource[].class);
            resources = returnedEntity.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        for (Resource res : resources) {
            resourceList.add(res);
        }
        return resourceList;
    }

    @Override
    public List<Resource> getPublicResourcesByCompanyId(final Long id) {
        HttpHeaders headers = new HttpHeaders();
        AuthenticationServiceImpl.setAuthenticationHeader(headers);
        HttpEntity<Resource> requestEntity = new HttpEntity<Resource>(null, headers);

        List<Resource> resourceList = new ArrayList<Resource>();
        Resource[] resources = null;
        try {
            ResponseEntity<Resource[]> returnedEntity = restTemplate.exchange(BASE_URL + "/"
                    + entityUri + "/Get/PublicResources/" + id, HttpMethod.GET, requestEntity,
                    Resource[].class);
            resources = returnedEntity.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        for (Resource res : resources) {
            resourceList.add(res);
        }
        return resourceList;
    }

    @Override
    public List<Resource> getAllResourcesFromOwnCompany() {
        HttpHeaders headers = new HttpHeaders();
        AuthenticationServiceImpl.setAuthenticationHeader(headers);
        HttpEntity<Resource> requestEntity = new HttpEntity<Resource>(null, headers);

        List<Resource> resourceList = new ArrayList<Resource>();
        Resource[] resources = null;
        try {
            ResponseEntity<Resource[]> returnedEntity = restTemplate.exchange(BASE_URL + "/"
                    + entityUri + "/Get/ResourcesFromOwnCompany", HttpMethod.GET, requestEntity,
                    Resource[].class);
            resources = returnedEntity.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        for (Resource res : resources) {
            resourceList.add(res);
        }
        return resourceList;
    }

    @Override
    public List<Resource> freeTextSearch(final String searchString, final int page) {
        HttpHeaders headers = new HttpHeaders();
        AuthenticationServiceImpl.setAuthenticationHeader(headers);
        HttpEntity<Resource> requestEntity = new HttpEntity<Resource>(null, headers);

        List<Resource> resourceList = new ArrayList<Resource>();
        Resource[] resources = null;
        try {
            ResponseEntity<Resource[]> returnedEntity = restTemplate.exchange(BASE_URL
                    + entityUri + "/Search?searchString=" + searchString + "&page=" + page,
                    HttpMethod.GET, requestEntity, Resource[].class);
            resources = returnedEntity.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        for (Resource res : resources) {
            resourceList.add(res);
        }
        return resourceList;
    }

}
